import React, { useEffect, useState } from 'react'
import { throttle } from '../../utils/tool'

const useGetLineInfo = (
	moveX,
	moveY,
	target,
	lineLeftNums,
	lineLeftMidNums,
	lineRightNums,
	lineTopNums,
	lineTopMidNums,
	lineBtmNums
) => {
	const [lineTop, setLineTop] = useState(0)
	const [lineLeft, setLineLeft] = useState(0)
	const [tFlag, setTFlag] = useState()
	const [lFlag, setLFlag] = useState()

	const getInfo = () => {
		if (!target) return

		const cTop = target.top + moveY
		const cMidT = target.top + moveY + target.height / 2
		const cBtm = target.top + moveY + target.height
		const cLeft = target.left + moveX
		const cMidW = target.left + moveX + target.width / 2
		const cRight = target.left + moveX + target.width
		let topFlag = [cMidT, cTop, cBtm].some((item) => {
			let flag = false
			if (lineTopMidNums.includes(item) || lineTopNums.includes(item) || lineBtmNums.includes(item)) {
				setLineTop(item)
				return true
			}
			return flag
		})
		setTFlag(topFlag)
		let leftFlag = [cMidW, cLeft, cRight].some((item) => {
			let flag = false
			if (lineLeftMidNums.includes(item) || lineLeftNums.includes(item) || lineRightNums.includes(item)) {
				setLineLeft(item)
				return true
			}
			return flag
		})
		setLFlag(leftFlag)
	}

	// const getInfo = throttle(() => {
	//   if (!target) return
	//
	//   const cTop = target.top + moveY
	//   const cMidT = target.top + moveY + target.height / 2
	//   const cBtm = target.top + moveY + target.height
	//   const cLeft = target.left + moveX
	//   const cMidW = target.left + moveX + target.width / 2
	//   const cRight = target.left + moveX + target.width
	//   let topFlag = [cMidT, cTop, cBtm].some(item => {
	//     let flag = false
	//     if (lineTopMidNums.includes(item) || lineTopNums.includes(item) || lineBtmNums.includes(item)) {
	//       setLineTop(item)
	//       return true
	//     }
	//     return flag
	//   })
	//   setTFlag(topFlag)
	//   let leftFlag = [cMidW, cLeft, cRight].some(item => {
	//     let flag = false
	//     if (lineLeftMidNums.includes(item) || lineLeftNums.includes(item) || lineRightNums.includes(item)) {
	//       setLineLeft(item)
	//       return true
	//     }
	//     return flag
	//   })
	//   setLFlag(leftFlag)
	// }, 32)

	useEffect(() => {
		;(async () => {
			await getInfo()
		})()
	}, [moveX, moveY, target, lineTopNums, lineLeftNums])

	return { lineTop, lineLeft, tFlag, lFlag }
}

export { useGetLineInfo }
